package com.ruoyi.basic.controller;

import java.util.List;

import com.ruoyi.api.system.utils.ShiroUtils;
import com.ruoyi.api.system.utils.poi.ExcelUtil;
import com.ruoyi.basic.domain.BasicPost;
import com.ruoyi.basic.domain.BasicUser;
import com.ruoyi.basic.service.IBasicUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.basic.domain.BasicReward;
import com.ruoyi.basic.service.IBasicRewardService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 年度考核奖励Controller
 * 
 * @author ruoyi
 * @date 2022-08-15
 */
@Controller
@RequestMapping("/basic/reward")
public class BasicRewardController extends BaseController
{
    private String prefix = "basic/reward";

    @Autowired
    private IBasicRewardService basicRewardService;

    @Autowired
    private IBasicUserService basicUserService;

    @RequiresPermissions("basic:reward:view")
    @GetMapping()
    public String reward()
    {
        return prefix + "/reward";
    }

    /**
     * 查询年度考核奖励列表
     */
    @RequiresPermissions("basic:reward:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BasicReward basicReward)
    {
        startPage();
        List<BasicReward> list = basicRewardService.selectBasicRewardList(basicReward);
        return getDataTable(list);
    }

    /**
     * 导出年度考核奖励列表
     */
    @RequiresPermissions("basic:reward:export")
    @Log(title = "年度考核奖励", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BasicReward basicReward)
    {
        List<BasicReward> list = basicRewardService.selectBasicRewardList(basicReward);
        ExcelUtil<BasicReward> util = new ExcelUtil<BasicReward>(BasicReward.class);
        return util.exportExcel(list, "年度考核奖励数据");
    }

    @Log(title = "年度考核奖励", businessType = BusinessType.IMPORT)
    @RequiresPermissions("basic:reward:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<BasicReward> util = new ExcelUtil<BasicReward>(BasicReward.class);
        List<BasicReward> rewardList = util.importExcel(file.getInputStream());
        String message = basicRewardService.importReward(rewardList, updateSupport, ShiroUtils.getLoginName());
        return AjaxResult.success(message);
    }

    @RequiresPermissions("basic:reward:import")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<BasicReward> util = new ExcelUtil<BasicReward>(BasicReward.class);
        return util.importTemplateExcel("年度考核奖励");
    }

    /**
     * 新增年度考核奖励
     */
    @GetMapping("/add/{userId}")
    public String add(@PathVariable("userId") Long userId, ModelMap mmap)
    {
        BasicUser user = basicUserService.selectBasicUserById(userId);
        mmap.put("user", user == null ? new BasicUser() : user);
        return prefix + "/add";
    }

    /**
     * 新增保存年度考核奖励
     */
    @RequiresPermissions("basic:reward:add")
    @Log(title = "年度考核奖励", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BasicReward basicReward)
    {
        basicReward.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(basicRewardService.insertBasicReward(basicReward));
    }

    /**
     * 修改年度考核奖励
     */
    @GetMapping("/edit/{rewardId}")
    public String edit(@PathVariable("rewardId") Long rewardId, ModelMap mmap)
    {
        BasicReward basicReward = basicRewardService.selectBasicRewardByRewardId(rewardId);
        mmap.put("basicReward", basicReward);
        return prefix + "/edit";
    }

    /**
     * 修改保存年度考核奖励
     */
    @RequiresPermissions("basic:reward:edit")
    @Log(title = "年度考核奖励", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BasicReward basicReward)
    {
        basicReward.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(basicRewardService.updateBasicReward(basicReward));
    }

    /**
     * 删除年度考核奖励
     */
    @RequiresPermissions("basic:reward:remove")
    @Log(title = "年度考核奖励", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(basicRewardService.deleteBasicRewardByRewardIds(ids));
    }
}
